import 'package:cached_network_image/cached_network_image.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:taonile/config/config.dart';
import 'package:taonile/entity/my_team_entity.dart';

import '../http/api.dart';
import '../routers/routers.dart';
import '../views/default_views.dart';

///
/// 我的团队
///
class MineTeam extends StatefulWidget {
  var userId = '';
  var isMe = 0;

  MineTeam({Key? key, this.userId = '', this.isMe = 0}) : super(key: key);

  @override
  State<MineTeam> createState() => _MineTeamState();
}

class _MineTeamState extends State<MineTeam> {
  int page = 1;
  MyTeamEntity entity = MyTeamEntity();
  late List<MyTeamDataList> datas = [];

  @override
  void initState() {
    super.initState();
    getDatas();
  }

  getDatas() async {
    dynamic result =
        await Api.getMineTeam({'page': page.toString(), 'limit': '20', 'id': widget.userId});
    entity = MyTeamEntity.fromJson(result);
    setState(() {
      if (page == 1) {
        datas.clear();
      }
      if (entity.data.list.isNotEmpty) {
        page++;
        datas.addAll(entity.data.list);
      }
    });
  }

  Widget getListItem(int index) {
    return Container(
      height: 180.w,
      padding: EdgeInsets.symmetric(vertical: 22.w, horizontal: 20.w),
      decoration: BoxDecoration(
        border: Border(bottom: BorderSide(width: 3.w, color: Config.colorDivider)),
      ),
      child: InkWell(
        onTap: () {
          Routes.navigateTo(context, Routes.mineTeam, params: {'userId': datas[index].id});
        },
        child: Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Container(
                  //头像区
                  margin: EdgeInsets.fromLTRB(0.w, 0.w, 0, 0),
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      ClipRRect(
                        borderRadius: BorderRadius.circular(32.w),
                        child: CachedNetworkImage(
                          imageUrl: datas[index].avatar,
                          fit: BoxFit.fill,
                          width: 64.w,
                          height: 64.w,
                          errorWidget: (context, url, error) {
                            return Image.asset(
                              'assets/images/icon_default_head.jpg',
                              width: 64.w,
                              height: 64.w,
                              fit: BoxFit.fill,
                            );
                          },
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
            Container(
              width: 560.w,
              margin: EdgeInsets.only(left: 20.w),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Row(
                        children: [
                          Text(
                            datas[index].name,
                            style: TextStyle(
                              fontSize: 24.w,
                              color: Config.colorText1,
                            ),
                          ),
                          Visibility(
                            visible: datas[index].isSlide == '1',
                            child: Container(
                              decoration: BoxDecoration(
                                color: Config.colorMain,
                                borderRadius: BorderRadius.circular(20.w),
                                border: Border.all(color: Config.colorMain, width: 2.w),
                              ),
                              padding: EdgeInsets.symmetric(vertical: 3.w, horizontal: 8.w),
                              margin: EdgeInsets.only(left: 10.w),
                              child: Text(
                                '滑落',
                                // entity.code == 1 ? entity.data.info.level : '',
                                style: TextStyle(fontSize: 15.sp, color: Config.colorWhite),
                              ),
                            ),
                          ),
                        ],
                      ),
                      Text(
                        datas[index].createdAt,
                        style: TextStyle(
                          fontSize: 22.w,
                          color: Config.colorText3,
                        ),
                      ),
                    ],
                  ),
                  Padding(padding: EdgeInsets.only(top: 12.w)),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      Visibility(
                        visible: entity.code == 1 && datas[index].levelName.isNotEmpty,
                        child: Container(
                          decoration: BoxDecoration(
                            color: Colors.transparent,
                            borderRadius: BorderRadius.circular(20.w),
                            border: Border.all(color: Config.colorMain, width: 2.w),
                          ),
                          padding: EdgeInsets.symmetric(vertical: 4.w, horizontal: 10.w),
                          margin: EdgeInsets.only(left: 0.w),
                          child: Text(
                            entity.code == 1 ? datas[index].levelName : '',
                            // entity.code == 1 ? entity.data.info.level : '',
                            style: TextStyle(fontSize: 15.sp, color: Config.colorMain),
                          ),
                        ),
                      ),
                      Visibility(
                        visible:
                            entity.code == 1 && datas[index].copartnerName.isNotEmpty,
                        child: Container(
                          decoration: BoxDecoration(
                            color: Colors.transparent,
                            borderRadius: BorderRadius.circular(20.w),
                            border: Border.all(color: Config.colorMain, width: 2.w),
                          ),
                          padding: EdgeInsets.symmetric(vertical: 4.w, horizontal: 10.w),
                          margin: EdgeInsets.only(left: 15.w),
                          child: Text(
                            entity.code == 1 ? datas[index].copartnerName : '',
                            style: TextStyle(fontSize: 15.sp, color: Config.colorMain),
                          ),
                        ),
                      ),
                    ],
                  ),
                  Padding(padding: EdgeInsets.only(top: 15.w)),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        "团队业绩：${datas[index].teamMoney}",
                        style: TextStyle(
                          fontSize: 24.w,
                          color: Config.colorText1,
                        ),
                      ),
                      Text(
                        "团队人数：${datas[index].teamCount}",
                        style: TextStyle(
                          fontSize: 22.w,
                          color: Config.colorText1,
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Config.colorBg2,
        appBar: AppBar(
          title: Text(widget.isMe == 1 ? '我的团队' : '团队详情'),
          centerTitle: true,
        ),
        body: SizedBox(
          width: double.infinity,
          height: double.infinity,
          child: Stack(
            children: [
              Container(
                width: double.infinity,
                height: 290.w,
                padding: EdgeInsets.symmetric(vertical: 20.w, horizontal: 20.w),
                margin: EdgeInsets.fromLTRB(30.w, 30.w, 30.w, 0),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(12.w),
                ),
                child: Column(
                  children: [
                    Column(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        Row(
                          children: [
                            Text(
                              widget.isMe == 1 ? "我的级别身份" : '级别身份',
                              style: TextStyle(fontSize: 24.sp, color: Config.colorText1),
                            ),
                            Visibility(
                              visible: entity.code == 1 && entity.data.info.levelName.isNotEmpty,
                              child: Container(
                                decoration: BoxDecoration(
                                  color: Colors.transparent,
                                  borderRadius: BorderRadius.circular(20.w),
                                  border: Border.all(color: Config.colorMain, width: 2.w),
                                ),
                                padding: EdgeInsets.symmetric(vertical: 4.w, horizontal: 10.w),
                                margin: EdgeInsets.only(left: 12.w),
                                child: Text(
                                  entity.data.info.levelName,
                                  style: TextStyle(fontSize: 15.sp, color: Config.colorMain),
                                ),
                              ),
                            ),
                            Visibility(
                              visible:
                                  entity.code == 1 && entity.data.info.copartnerName.isNotEmpty,
                              child: Container(
                                decoration: BoxDecoration(
                                  color: Colors.transparent,
                                  borderRadius: BorderRadius.circular(20.w),
                                  border: Border.all(color: Config.colorMain, width: 2.w),
                                ),
                                padding: EdgeInsets.symmetric(vertical: 4.w, horizontal: 10.w),
                                margin: EdgeInsets.only(left: 12.w),
                                child: Text(
                                  entity.data.info.copartnerName,
                                  style: TextStyle(fontSize: 15.sp, color: Config.colorMain),
                                ),
                              ),
                            ),
                          ],
                        ),
                        Padding(
                          padding: EdgeInsets.only(top: 24.w),
                        ),
                        Divider(
                          color: Config.colorDivider,
                          height: 2.w,
                          thickness: 2.w,
                        ),
                        Padding(
                          padding: EdgeInsets.only(top: 15.w),
                        ),
                        Text(
                          "团队总业绩",
                          style: TextStyle(fontSize: 22.sp),
                        ),
                        Text(
                          entity.code == 1 ? entity.data.info.teamMoney : '',
                          style: TextStyle(fontSize: 26.sp),
                        ),
                        Padding(
                          padding: EdgeInsets.only(top: 15.w),
                        ),
                        Divider(
                          color: Config.colorDivider,
                          height: 2.w,
                          thickness: 2.w,
                        ),
                        Padding(
                          padding: EdgeInsets.only(top: 15.w),
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceAround,
                          children: [
                            Column(
                              children: [
                                Text(
                                  "团队总人数",
                                  style: TextStyle(fontSize: 22.sp),
                                ),
                                Text(
                                  entity.code == 1 ? entity.data.info.teamCount : '',
                                  style: TextStyle(fontSize: 26.sp),
                                ),
                              ],
                            ),
                            Column(
                              children: [
                                Text(
                                  "今日新增业绩",
                                  style: TextStyle(fontSize: 22.sp),
                                ),
                                Text(
                                  entity.code == 1 ? entity.data.info.todayMoney : '',
                                  style: TextStyle(fontSize: 26.sp),
                                ),
                              ],
                            ),
                          ],
                        ),
                      ],
                    ),
                  ],
                ),
              ),
              Container(
                width: double.infinity,
                height: double.infinity,
                padding: EdgeInsets.symmetric(vertical: 0, horizontal: 0.w),
                margin: EdgeInsets.only(left: 30.w, right: 30.w, top: 345.w, bottom: 25.w),
                decoration: BoxDecoration(
                  color: (Config.colorWhite),
                  borderRadius: BorderRadius.circular(12.w),
                ),
                child: datas.isNotEmpty
                    ? EasyRefresh(
                        header: const MaterialHeader(),
                        footer: const MaterialFooter(),
                        onRefresh: () async {
                          await Future.delayed(const Duration(milliseconds: 200), () {
                            page = 1;
                            getDatas();
                          });
                        },
                        onLoad: () async {
                          getDatas();
                        },
                        child: ListView.builder(
                          // physics: const NeverScrollableScrollPhysics(),
                          itemCount: datas.length,
                          padding: EdgeInsets.symmetric(vertical: 1.w, horizontal: 1.w),
                          itemBuilder: (BuildContext context, int index) {
                            return getListItem(index);
                          },
                        ),
                      )
                    : showNullData(),
              ),
            ],
          ),
        ));
  }
}
